package com.example.blog.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.blog.dao.mapper.ArticleMapper;
import com.example.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {

    @Async("taskExecutor")

    //此操作在线程池里面执行 不影响原有的主线程
    public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
        int viewCounts = article.getViewCounts();
        Article articleUpdate = new Article();
        articleUpdate.setViewCounts(viewCounts+1);

        LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Article::getAid,article.getAid());
        //设置一个 为了在多线程的环境下 线程安全
        updateWrapper.eq(Article::getViewCounts,viewCounts);
        //update
        articleMapper.update(articleUpdate,updateWrapper);
        try{

            Thread.sleep(1000);
            System.out.println("更新完成了。。。。。。。。。。");

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public void updateArticleLikes(ArticleMapper articleMapper, Article article) {
        int likesCounts = article.getLikesCounts();
        Article articleUpdate = new Article();
        articleUpdate.setLikesCounts(likesCounts+1);

        LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Article::getAid,article.getAid());
        //设置一个 为了在多线程的环境下 线程安全
        updateWrapper.eq(Article::getLikesCounts,likesCounts);
        //update
        articleMapper.update(articleUpdate,updateWrapper);
        try{

            Thread.sleep(1000);
            System.out.println("点赞更新完成了。。。。。。。。。。");

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
